cd ${work}5_reg_analysis

*** CONSTRUCT TABLE 1
*(Table 1 is constructed manually based on the output in output\table_1.log)

qui {
log using output\table_1 , text replace

*** PANEL A
use input\sample_idt, clear

*EXPORT STATUS
preserve
	use input\export_ipdt, clear
	keep if vrd_exp_exsmall!=.
	keep jurnr land year
	duplicates drop
	save temp\temp, replace
restore
merge 1:1 jurnr land year using temp\temp, keep(1 3) keepusing(jurnr)
erase temp\temp.dta
gen exp=(_m==3)
drop _m

*PROMOTION
destring jurnr, gen(jurnr_num)
merge 1:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist treated_1 tdcinitiative_1 {
    replace `v'=0 if `v'==.
}

gen 	group = 1 if treated_1==0 & tdcinitiative_1==0
replace	group = 2 if treated_1==0 & tdcinitiative_1==1
replace	group = 3 if treated_1==1 & tdcinitiative_1==0
replace	group = 4 if treated_1==1 & tdcinitiative_1==1

noi disp "PANEL A"
noi tabstat exp, by(group) s(mean sd) f(%9.3f)
noi tab group



*** PANEL B
use input\sample_idt, clear

*EXPORT SALES
preserve
	use input\export_ipdt, clear
	keep if vrd_exp_exsmall!=.
	collapse (sum) vrd_exp_exsmall, by(jurnr land year)
	save temp\temp, replace
restore
merge 1:1 jurnr land year using temp\temp, keep(3) keepusing(vrd_exp_exsmall) nogen
erase temp\temp.dta
gen log_exp_sales=log(vrd_exp_exsmall)
winsor2 log_exp_sales, cuts(0 99) replace

*PROMOTION
destring jurnr, gen(jurnr_num)
merge 1:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist treated_1 tdcinitiative_1 {
    replace `v'=0 if `v'==.
}

gen 	group = 1 if treated_1==0 & tdcinitiative_1==0
replace	group = 2 if treated_1==0 & tdcinitiative_1==1
replace	group = 3 if treated_1==1 & tdcinitiative_1==0
replace	group = 4 if treated_1==1 & tdcinitiative_1==1

noi disp "PANEL B"
noi tabstat log_exp_sales, by(group) s(mean sd) f(%9.3f)
noi tab group



*** PANEL C
use input\export_ipdt, clear
keep jurnr vnr land year vrd_exp_exsmall quantity
keep if vrd_exp_exsmall!=.
replace vrd_exp_exsmall = vrd_exp_exsmall/1e3

gen log_exp_sales=log(vrd_exp_exsmall)
gen lquantity=log(quantity)		
gen lprice=log(vrd_exp_exsmall/quantity)

foreach v of varlist lquantity lprice {
	qui reghdfe `v', a(vnr) res(res)
	replace `v'=res
	drop res
}

winsor2 log_exp_sales, cuts(0 99) replace
winsor2 lquantity lprice, cuts(1 99) replace

foreach v of varlist lquantity lprice {
    qui sum `v'
	replace `v'=`v'-r(mean)
}

*PROMOTION
destring jurnr, gen(jurnr_num)
merge m:1 jurnr_num land year using input\promotion_idt, keep(1 3) keepusing(treated_1 tdcinitiative_1) nogen
drop jurnr_num
foreach v of varlist treated_1 tdcinitiative_1 {
    replace `v'=0 if `v'==.
}

gen 	group = 1 if treated_1==0 & tdcinitiative_1==0
replace	group = 2 if treated_1==0 & tdcinitiative_1==1
replace	group = 3 if treated_1==1 & tdcinitiative_1==0
replace	group = 4 if treated_1==1 & tdcinitiative_1==1

noi disp "PANEL C"
noi tabstat log_exp_sales lquantity lprice, by(group) s(mean sd) f(%9.3f)
noi tab group

log close
}